package com.lianziyou.chain.comm;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.Hash;
import org.web3j.crypto.Sign;
import org.web3j.crypto.StructuredData;
import org.web3j.utils.Numeric;

@Slf4j
public class Web3SignUtil {

    @Test
    void testSignData() {
        String contractAddress = "FEF4138d936Baf22303898adeFE3FbfEA9572439";
        String toAddress = "46BDF0c335B05B92bad417E0B817B1f51B454A6e";
        String orderNo = Hash.sha3String(Numeric.toHexString(UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8)));
        BigInteger tokenId = BigInteger.valueOf(1);
        BigInteger amount = BigInteger.valueOf(1);
        BigInteger deadline = BigInteger.valueOf(System.currentTimeMillis()/1000+3600);
        BigInteger chainId = BigInteger.valueOf(56);

        log.info("orderNo={}", orderNo);

        log.info("dealline={}", deadline);

        log.info("contractAddress={}", Numeric.toHexStringNoPrefixZeroPadded(new BigInteger(contractAddress, 16), 64));

        String messageStr = "0x" + Numeric.toHexStringNoPrefixZeroPadded(new BigInteger(contractAddress, 16), 64)
            + Numeric.toHexStringNoPrefixZeroPadded(new BigInteger(toAddress, 16), 64)
            + orderNo.substring(2)
            + Numeric.toHexStringNoPrefixZeroPadded(tokenId, 64)
            + Numeric.toHexStringNoPrefixZeroPadded(amount, 64)
            + Numeric.toHexStringNoPrefixZeroPadded(deadline, 64)
            + Numeric.toHexStringNoPrefixZeroPadded(chainId, 64);
        System.out.println("message data: " + messageStr);

        byte[] messageSha3 = Hash.sha3(Numeric.hexStringToByteArray(messageStr));
        System.out.println("message sha3: " + Numeric.toHexString(messageSha3));

        Credentials credentials = Credentials.create("fbd09ad9b076091e18ca5ac72555b5dd96ec37973e328142db124da0de6e896a");

        Sign.SignatureData signatureData = Sign.signMessage(Numeric.hexStringToByteArray(messageStr), credentials.getEcKeyPair());


        System.out.println("v: " + Numeric.toBigInt(signatureData.getV()));
        System.out.println("r: " + Numeric.toHexString(signatureData.getR()));
        System.out.println("s: " + Numeric.toHexString(signatureData.getS()));

    }

    /**
     * 众筹
     */
    @Test
    void testSignDataZhongchou() {
        String contractAddress = "0x52fa964A569E9d2E14d8412EA4963bDbD38999Ac";
        String toAddress = "0x4917C5C7d35FB0D9F758476A64a3e32bAE088c64";
        String orderNo = Hash.sha3String(Numeric.toHexString(UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8)));
        BigInteger amount = new BigInteger("1000000000000000000");
        BigInteger chainId = BigInteger.valueOf(888);

        log.info("contractAddress={}", Numeric.toHexStringNoPrefixZeroPadded(new BigInteger(contractAddress.substring(2), 16), 64));

        log.info("orderNo={}", orderNo);

        log.info("amount={}", amount);

        String messageStr = "0x" + Numeric.toHexStringNoPrefixZeroPadded(new BigInteger(contractAddress.substring(2), 16), 64)
            + Numeric.toHexStringNoPrefixZeroPadded(new BigInteger(toAddress.substring(2), 16), 64)
            + orderNo.substring(2)
            + Numeric.toHexStringNoPrefixZeroPadded(amount, 64)
            + Numeric.toHexStringNoPrefixZeroPadded(chainId, 64);
        System.out.println("message data: " + messageStr);

        byte[] messageSha3 = Hash.sha3(Numeric.hexStringToByteArray(messageStr));
        System.out.println("message sha3: " + Numeric.toHexString(messageSha3));

        Credentials credentials = Credentials.create("e43f0e11c7941d963c3387a34f561cbdbac96901096ce805a53e0f3b60fca160");

        Sign.SignatureData signatureData = Sign.signMessage(Numeric.hexStringToByteArray(messageStr), credentials.getEcKeyPair());


        System.out.println("v: " + Numeric.toBigInt(signatureData.getV()));
        System.out.println("r: " + Numeric.toHexString(signatureData.getR()));
        System.out.println("s: " + Numeric.toHexString(signatureData.getS()));

    }


    @Test
    void testEIP712SignData() {
//        StructuredData structuredData = new StructuredData();


     log.info( "{}",new BigDecimal("1111.2121312112312").multiply(BigDecimal.TEN.pow(18)).toBigInteger());

    }
}
